import fetch from 'common/src/utils/fetch'

// 查询指定分页的课程分类
export function getCourseCategoryList(params) {
  let query = '?'
  for (const key in params) {
    query += key
    query += '='
    query += params[key]
    query += '&'
  }
  return fetch({
    url: '/api/cultureservice/coursesLabel/page' + query,
    method: 'get'
  })
}
// 查询课程分类
export function getCourseAllList(params) {
  let query = '?'
  for (const key in params) {
    query += key
    query += '='
    query += params[key]
    query += '&'
  }
  return fetch({
    url: '/api/cultureservice/coursesLabel/coursesLabelList' + query,
    method: 'get'
  })
}

// 新增课程分类
export function addCourseLabel(data) {
  return fetch({
    url: '/api/cultureservice/coursesLabel',
    method: 'post',
    headers: {
      'Content-Type': 'application/json'
    },
    data
  })
}
// 删除课程分类
export function deleteCourseById(ids) {
  return fetch({
    url: '/api/cultureservice/coursesLabel',
    method: 'delete',
    headers: {
      'Content-Type': 'application/json'
    },
    data: {
      'idSet': ids
    }
  })
}
// 根据Id查看课程分类详情
export function courseInfoById(id) {
  return fetch({
    url: 'api/cultureservice/coursesLabel/' + id,
    method: 'GET'
  })
}
// 更新课程分类
export function updateCourse(data) {
  return fetch({
    url: '/api/cultureservice/coursesLabel',
    method: 'PUT',
    headers: {
      'Content-Type': 'application/json'
    },
    data
  })
}

// 查询培养模板列表
export function getCultureTemplateList(params) {
  let query = '?'
  for (const key in params) {
    query += key
    query += '='
    query += params[key]
    query += '&'
  }
  return fetch({
    url: '/api/cultureservice/cultureTemplate/page' + query,
    method: 'get'
  })
}
// 保存模板
export function saveCultureTemplate(data) {
  let newData = Object.assign({}, data);
  if (newData.trainingCategory && newData.trainingCategory instanceof Array) {
    newData.trainingCategory = newData.trainingCategory.join(",");
  }
  return fetch({
    url: '/api/cultureservice/cultureTemplate',
    method: 'post',
    headers: {
      'Content-Type': 'application/json'
    },
    data: newData
  })
}
// 根据id查询培养方案模板基本信息
export function getCultureTemplateById(id) {
  return fetch({
    url: '/api/cultureservice/cultureTemplate/' + id,
    method: 'get'
  })
}
// 删除培养方案模板
export function deleteCultureTemplateById(ids) {
  return fetch({
    url: '/api/cultureservice/cultureTemplate',
    method: 'DELETE',
    headers: {
      'Content-Type': 'application/json'
    },
    data: {
      'idSet': ids
    }
  })
}
// 模板（方案）添加或者修改课程类别表单
export function saveCourseCategoryForm(data) {
  return fetch({
    url: '/api/cultureservice/cultureRule/batchUpdCultureRule',
    method: 'POST',
    headers: {
      'Content-Type': 'application/json'
    },
    data
  })
}
// 根据模板（方案）id，查询出课程类别表单
export function getCourseCategoryFormById(id, type) {
  return fetch({
    url: '/api/cultureservice/cultureTemplate/coursesLabelList/' + id + '?type=' + type,
    method: 'GET'
  })
}
// 根据条件，分页查询课程
export function getCourseList(data) {
  return fetch({
    url: '/api/cultureservice/courses/page',
    method: 'POST',
    headers: {
      'Content-Type': 'application/json'
    },
    data
  })
}
// 接口有变化
export function getCourseListNew(data) {
  return fetch({
    url: '/api/cultureservice/courses/TrainingProgram',
    method: 'POST',
    headers: {
      'Content-Type': 'application/json'
    },
    data
  })
}
// 根据开课学期、性质、学院等查询课程列表
export function queryCourseList(data) {
  return fetch({
    url: '/api/cultureservice/courses/queryNormalCourseList',
    method: 'POST',
    headers: {
      'Content-Type': 'application/json'
    },
    data
  })
}
// 根据开课学期、性质、学院等查询补休课课程列表
export function queryAddCourseList(data) {
  return fetch({
    url: '/api/cultureservice/courses/queryAddCourseList',
    method: 'POST',
    headers: {
      'Content-Type': 'application/json'
    },
    data
  })
}
/**
 * 根据code查询课程详情
 */
export function getCourseByCode(data) {
  return fetch({
    url: '/api/cultureservice/courses/list',
    method: 'POST',
    headers: {
      'Content-Type': 'application/json'
    },
    data
  })
}
// 根据课程id，查询所属培养方案
export function queryScheme(coursesCode, pageNum_, pageSize_) {
  return fetch({
    url: 'api/cultureservice/courses/cultureScheme/' + coursesCode + '?pageNum_=' + pageNum_ + '&pageSize_=' + pageSize_,
    method: 'GET'
  })
}

// 获取学院ids
export function getCollegeids() {
  return fetch({
    url: 'api/cultureservice/courses/colleges',
    method: 'GET'
  })
}

// 自动生成课程code
export function autoCode(data) {
  return fetch({
    url: 'api/cultureservice/courses/autoCode',
    method: 'POST',
    headers: {
      'Content-Type': 'application/json'
    },
    data
  })
}

// 上传模板
export function upload(path, managerDeptId) {
  return fetch({
    url: 'api/cultureservice/courses/template/upload?path=' + path + "&&managerDeptId=" + managerDeptId,
    method: 'POST'
  })
}

// 获得路径
export function getpath(managerDeptId) {
  return fetch({
    url: 'api/cultureservice/courses/template/path?managerDeptId=' + managerDeptId,
    method: 'GET'
  })
}

// 下载模板
export function down(path) {
  return fetch({
    url: '/api/commonservice/obsfile/downloadimage?objectkey=' + path,
    method: 'GET'
  })
}

// 根据模板（方案）id查询课程设置（课程表单）
export function courseLabelRelation(templateId, type) {
  return fetch({
    url: '/api/cultureservice/courseLabelRelation/list/' + templateId + '?type=' + type,
    method: 'GET'
  })
}
// 增加课程设置（课程表单）
export function addCourse(data) {
  return fetch({
    url: '/api/cultureservice/courseLabelRelation',
    method: 'POST',
    headers: {
      'Content-Type': 'application/json'
    },
    data
  })
}
// 修改课程设置（课程表单）
export function EditCourse(data) {
  return fetch({
    url: '/api/cultureservice/courseLabelRelation',
    method: 'PUT',
    headers: {
      'Content-Type': 'application/json'
    },
    data
  })
}
// 删除课程设置（课程表单）
export function deleteCourse(id) {
  return fetch({
    url: '/api/cultureservice/courseLabelRelation/' + id,
    method: 'DELETE',
    headers: {
      'Content-Type': 'application/json'
    },
    data: {
      id: id
    }
  })
}
// 发布培养方案模板
export function pushCultureTemplateById(ids) {
  return fetch({
    url: '/api/cultureservice/cultureTemplate/release',
    method: 'PUT',
    headers: {
      'Content-Type': 'application/json'
    },
    data: {
      'idSet': ids
    }
  })
}

// （树形）课程类别列表条件查询，不分页
export function coursesLabelTreeList(params) {
  return fetch({
    url: '/api/cultureservice/coursesLabel/coursesLabelTreeList',
    method: 'GET',
    params
  })
}

// 删除课程设置（课程表单）
export function batchSaveCultureRule(data) {
  return fetch({
    url: '/api/cultureservice/cultureRule/batchSaveCultureRule',
    method: 'POST',
    data
  })
}

// 查询模板已勾选的课程类别
export function findCheckedCultureLabelList(data) {
  return fetch({
    url: '/api/cultureservice/cultureRule/findCheckedCultureLabelList',
    method: 'POST',
    data
  })
}

// 复制培养方案模板
export function copyCultureTemplate(data) {
  return fetch({
    url: '/api/cultureservice/cultureTemplate/copyCultureTemplate',
    method: 'POST',
    data
  })
}

// 复制之前（课程设置）
export function copyBackCourseLabelRelation(data) {
  return fetch({
    url: '/api/cultureservice/courseLabelRelation/copyBackCourseLabelRelation',
    method: 'POST',
    data
  })
}

// 复制全部（课程设置）
export function copyBackAllCourseLabelRelation(data) {
  return fetch({
    url: '/api/cultureservice/courseLabelRelation/copyBackAllCourseLabelRelation',
    method: 'POST',
    data
  })
}

// 新增课程分组信息
export function addCourseLabelRelGroup(data) {
  return fetch({
    url: '/api/cultureservice/courseLabelRelGroup/addCourseLabelRelGroup',
    method: 'POST',
    data
  })
}
// 查询课程分组成员
export function getCourseLabelRelGroupMember(id, type) {
  return fetch({
    url: '/api/cultureservice/courseLabelRelation/getCourseLabelRelGroupMember?parentId=' + id + '&type=' + type,
    method: 'GET'
  })
}
/**
 * 查询某年某学院某专业的培养方案列表
 * @param data
 */
export function findCultureSchemeListByYear(data) {
  let newData = Object.assign({}, data);
  if (newData.trainingCategory && newData.trainingCategory instanceof Array) {
    newData.trainingCategory = newData.trainingCategory.join(",");
  }
  return fetch({
    url: '/api/cultureservice/cultureScheme/findCultureSchemeListByYear',
    method: 'post',
    data: newData
  })
}

/**
 * 获取课程分类
 * @param {获取课程分类后，回调函数} fn
 */
export function handleCourseCategory(fn, trainingLevel = 0) {
  let url = '/api/cultureservice/coursesLabel/coursesLabelList'
  if (trainingLevel) {
    url += '?trainingLevel=' + trainingLevel
  }
  fetch({
    url: url,
    method: 'get'
  }).then(res => {
    if (res.code === 200) {
      const result = []
      result.push({
        label: '',
        value: '',
        indent: 0,
        level: 0
      })
      if (res.data.length > 0) {
        loadCategoryRelation(result, res.data, 0)
      }
      if (fn) {
        fn(result)
      }
    }
  })
}
/**
 * 计算课程分类父子关系
 * @param {*层级关系的数组} array
 * @param {*课程分类数据源} dataSource
 * @param {*父目录id} parentId
 * @param {*过滤id} filterId
 * @param {*缩进距离} indent
 * @param {*当前层级} level
 */
function loadCategoryRelation(array = [],
  dataSource,
  parentId = 0,
  filterId = -1,
  indent = 0,
  level = 1) {
  dataSource.forEach(p => {
    if (p.parentId === parentId && p.id !== filterId) {
      array.push({
        label: p.label,
        value: p.id,
        indent: indent,
        level: level
      })
      loadCategoryRelation(
        array,
        dataSource,
        p.id,
        filterId,
        indent + 20,
        level + 1
      )
    }
  })
}

/**
 * 查询课程信息
 * @param data
 */
export function findCourseBycode(code) {
  return fetch({
    url: '/api/cultureservice/courses/findCourseByCode?code=' + code,
    method: 'get'
  })
}

/**
 * 查询所有满足code条件的课程信息
 * @param data
 */
export function getCoursesBycode(pageNum_, pageSize_, data) {
  return fetch({
    url: '/api/cultureservice/courses/getCoursesBycode?pageNum_=' + pageNum_ + "&pageSize_=" + pageSize_,
    method: 'POST',
    data
  })
}



/**
 * 模糊查询课程
 * @param data
 */
export function findCourseNameOrCodeList(code) {
  return fetch({
    url: '/api/cultureservice/courses/findCourseNameOrCodeList?condition=' + code,
    method: 'get'
  })
}
//撤销已发布的
export function revertCultureTemplateById(id = '') {
  return fetch({
    url: `/api/cultureservice/cultureScheme/updateSchemeStatus?id=${id}`,
    method: 'get'
  })
}
